/**
 * 
 */
package cn.com.xf.web.controller;

import java.text.SimpleDateFormat;
import java.text.Normalizer.Form;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;

import cn.com.xf.common.AbstractBaseController;
import cn.com.xf.common.Constants;
import cn.com.xf.common.StringUtil;
import cn.com.xf.domain.Tbdamagedgoods;
import cn.com.xf.domain.Tbdamagedimage;
import cn.com.xf.domain.Tbdamagedunknown;
import cn.com.xf.domain.TblOaAreaCoordinate;
import cn.com.xf.domain.TblOaFaq;
import cn.com.xf.domain.TblRegVisitor;
import cn.com.xf.domain.TsSiteinfo;
import cn.com.xf.domain.TsUser;
import cn.com.xf.service.DamagedgoodsService;
import cn.com.xf.service.FaqService;
import cn.com.xf.service.SettingService;
import cn.com.xf.utils.DateUtils;
import cn.com.xf.utils.encode.EncrypTool;
import cn.com.xf.web.PageModel;
import cn.com.xf.web.XFMappingJacksonJsonView;
import cn.com.xf.web.form.AreaForm;
import cn.com.xf.web.form.DamagedgoodsForm;
import cn.com.xf.web.form.DamagedimageFrom;
import cn.com.xf.web.form.DamagedunknownFrom;
import cn.com.xf.web.form.FaqForm;
import cn.com.xf.web.form.VisitorForm;
import cn.com.xf.web.security.UserInfo;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * @author Administrator
 * 
 */
@Controller
@RequestMapping("/faq")
public class FaqController extends AbstractBaseController {
	@Autowired
	private FaqService faqService;
	@Autowired
	private SettingService settingService;
	@Autowired
	private DamagedgoodsService damagedService;
	
	@RequestMapping
	public ModelAndView index(HttpServletRequest request, HttpServletResponse response, FaqForm form) throws Exception {
		ModelAndView mv = new ModelAndView("/faq/index", Constants.FORM_COMMAND_KEY, form);
		form.setTab(Constants.TAB_FAQ);
		return mv;
	}

	@RequestMapping(value = "/faql")
	public ModelAndView news(HttpServletRequest request, HttpServletResponse response, FaqForm form) throws Exception {
		ModelAndView mv = new ModelAndView("/faq/faq", Constants.FORM_COMMAND_KEY, form);
		form.setTab(Constants.TAB_FAQ);
		mv.addObject("defaultFaqClass",Constants.FAQ_CLASS_QA);
		return mv;
	}		
	
	@RequestMapping(value = "/list")
	public ModelAndView soft_list(HttpServletRequest request, HttpServletResponse response, FaqForm form)
			throws Exception {
		ModelAndView mv = new ModelAndView("/faq/faq_list", Constants.FORM_COMMAND_KEY, form);

		// 1.参数
		String faqClass = null;
		if (StringUtils.isNotBlank(form.getFaqClass())) {
			faqClass = form.getFaqClass();
		}

		// 2.总记录数
		int count = faqService.selectCount(null, null, faqClass);
		if (count == 0) {
			return mv;
		}

		// 3.分页信息
		int pageNum = 1;
		if (StringUtils.isNotBlank(form.getPageNum())) {
			pageNum = Integer.valueOf(form.getPageNum());
		}
		PageModel pageModel = new PageModel(count, PageModel.DEFAULT_PAGE_SIZE, pageNum, form);
		int begin = pageModel.getBegin();
		int pageSize = pageModel.getPageSize();

		// 4.排序规则
		String orderByField = "PUBLIC_TIME";
		String orderByMode = "DESC";
		if (StringUtils.isNotBlank(form.getOrderByField()) && StringUtils.isNotBlank(form.getOrderByMode())) {
			if ("title".equalsIgnoreCase(form.getOrderByField())) {
				orderByField = "title";
			}
			if ("asc".equalsIgnoreCase(form.getOrderByMode()) || "desc".equalsIgnoreCase(form.getOrderByMode())) {
				orderByMode = form.getOrderByMode();
			}
		}

		// 5.查询
		List<TblOaFaq> list = faqService.select(begin, pageSize, orderByField, orderByMode, faqClass);

		mv.addObject("list", list);
		return mv;
	}

	/**
	 * 详细页
	 * 
	 * @param request
	 * @param response
	 * @param form
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/detail")
	public ModelAndView faq_detial(HttpServletRequest request, HttpServletResponse response, FaqForm form)
			throws Exception {
		form.setTab(Constants.TAB_FAQ);
		ModelAndView mv = new ModelAndView("/faq/faq_detail", Constants.FORM_COMMAND_KEY, form);
		int faqId = Integer.valueOf(EncrypTool.dencode(form.getFaqId()));
		TblOaFaq coreFaq = faqService.selectById(faqId);
		mv.addObject("coreFaq", coreFaq);
		return mv;
	}

	/**
	 * 添加记录
	 */
	@RequestMapping(value = "/add")
	public ModelAndView faq_add(HttpServletRequest request, HttpServletResponse response, FaqForm form)
			throws Exception {
		String faqClass = form.getFaqClass();
		ModelAndView mv = new ModelAndView("/faq/faq_add", Constants.FORM_COMMAND_KEY, form);
		mv.addObject("faqClass",faqClass);
		return mv;
	}

	@RequestMapping(value = "/edit")
	public ModelAndView faq_edit(HttpServletRequest request, HttpServletResponse response, FaqForm form)
			throws Exception {
		ModelAndView mv = new ModelAndView("/faq/faq_edit", Constants.FORM_COMMAND_KEY, form);
		String faqId = form.getFaqId();
		TblOaFaq faq =  faqService.selectById(Integer.valueOf(faqId));
		mv.addObject("faq",faq);
		return mv;
	}
	/**
	 * 添加文件完成上传文件
	 * 
	 * @param request
	 * @param response
	 * @param form
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/doAdd")
	public ModelAndView faq_doAdd(HttpServletRequest request, HttpServletResponse response, @Valid FaqForm form,
			BindingResult result)
			throws Exception {
		ModelAndView mv = new ModelAndView("/faq/faq_add_result", Constants.FORM_COMMAND_KEY, form);
		List<ObjectError> errorList = null;
		if (result.hasErrors()) {
			errorList = result.getAllErrors();
			// 将错误信息Json化
			String errorJson = errorToJson(errorList);
			mv.addObject("errorJson", errorJson);
			return mv;
		}
		TblOaFaq coreFaq = form.copyTo();
		faqService.insert(coreFaq);
		mv.addObject("status", "success");
		return mv;

	}
	
	@RequestMapping(value = "/doEdit")
	public ModelAndView faq_doEdit(HttpServletRequest request, HttpServletResponse response, @Valid FaqForm form,
			BindingResult result)
			throws Exception {
		ModelAndView mv = new ModelAndView("/faq/faq_edit_result", Constants.FORM_COMMAND_KEY, form);
		List<ObjectError> errorList = null;
		if (result.hasErrors()) {
			errorList = result.getAllErrors();
			// 将错误信息Json化
			String errorJson = errorToJson(errorList);
			mv.addObject("errorJson", errorJson);
			return mv;
		}
		TblOaFaq coreFaq = form.copyTo();
		coreFaq.setFaqId(Integer.valueOf(form.getFaqId()));
		faqService.update(coreFaq);
		mv.addObject("status","success");
		return mv;
	}	
	
	
	@RequestMapping(value = "/manageFaqList")
	public ModelAndView faq_manage_list(HttpServletRequest request,HttpServletResponse response,FaqForm form)
		throws Exception {
		ModelAndView mv = new ModelAndView("/faq/faq_manage_list", Constants.FORM_COMMAND_KEY, form);

		String faqClass = StringUtil.StringFilter(form.getFaqClass());
		String faqQuestion = StringUtil.StringFilter(form.getFaqQuestion());
		String publicMan = StringUtil.StringFilter(form.getPublicMan());
		String createDateBegin = form.getCreateDateBegin();
		String createDateEnd = form.getCreateDateEnd();
		
		form.setCreateDateBegin(createDateBegin);
		form.setCreateDateEnd(createDateEnd);
		form.setFaqClass(faqClass);
		form.setFaqQuestion(faqQuestion);
		form.setPublicMan(publicMan);
		if (!StringUtils.isNotBlank(faqClass)) {
			faqClass = null;
		}
		// 2.总记录数
		int count = faqService.selectFaqListCount(faqClass, faqQuestion, publicMan,  createDateBegin, createDateEnd);
		if (count == 0) {
			return mv;
		}

		// 3.分页信息
		int pageNum = 1;
		if (StringUtils.isNotBlank(form.getPageNum())) {
			pageNum = Integer.valueOf(form.getPageNum());
		}
		PageModel pageModel = new PageModel(count, PageModel.DEFAULT_PAGE_SIZE, pageNum, form);
		int begin = pageModel.getBegin();
		int pageSize = pageModel.getPageSize();

		// 4.排序规则
		String orderByField = "PUBLIC_TIME";
		String orderByMode = "DESC";
		if (StringUtils.isNotBlank(form.getOrderByField()) && StringUtils.isNotBlank(form.getOrderByMode())) {
			if("publicTime".equalsIgnoreCase(form.getOrderByField())){
				orderByField = "PUBLIC_TIME";
			}				
			if ("faqQuestion".equalsIgnoreCase(form.getOrderByField())) {
				orderByField = "FAQ_QUESTION";
			}
			if("faqClass".equalsIgnoreCase(form.getOrderByField())){
				orderByField = "FAQ_CLASS";
			}
			if("publicMan".equalsIgnoreCase(form.getOrderByField())){
				orderByField = "PUBLIC_MAN";
			}
			if ("asc".equalsIgnoreCase(form.getOrderByMode()) || "desc".equalsIgnoreCase(form.getOrderByMode())) {
				orderByMode = form.getOrderByMode();
			}
		}

		// 5.查询
		List<TblOaFaq> list = faqService.select(begin, pageSize, orderByField, orderByMode, faqClass, faqQuestion, publicMan, createDateBegin, createDateEnd);
		mv.addObject("list", list);
		return mv;
	}
	
	@RequestMapping(value = "/deleteFaq")
	public ModelAndView faq_delete(HttpServletRequest request, HttpServletResponse response, FaqForm form)
			throws Exception {
		String faqId = form.getFaqId();
		int result = faqService.deleteFaqById(Integer.valueOf(faqId));
		Map<String, String> map = new HashMap<String, String>();
		if (result == -1) {
			map.put("status", "FAILD");
		} else if (result == 1) {
			map.put("status", "SUCCESS");
		}
		ModelAndView mv = new ModelAndView(new XFMappingJacksonJsonView(), map);
		return mv;
	}
	

	
	
}
